"use strict"
const { questionnaire, QSSelect, questionnaireAnswers, users } = require('../models');
const dateTime = require('../dateTime');

// 已发布的调查问卷
var fn_userPubishQS = async (ctx, next) => {
    var userId = ctx.session.userId;
    var username = ctx.session.userName;
    var currentUserQS = JSON.parse(JSON.stringify(await questionnaire.findAll({
        where: {
            publishUserId: userId,
            state:'1'
        }
    })));
    console.log(currentUserQS);
    // console.log(currentUserQS.createdAt);

    var allAnswers = JSON.parse(JSON.stringify(await questionnaireAnswers.findAll({})));

    // 回答人数
    currentUserQS.forEach(QS => {
        // console.log(QS);
        // console.log(QS.id);
        QS.userSum = allAnswers.filter(item => {
            return item.questionnaireId === QS.id
        }).length;
        // console.log(QS.userSum);
    })


    // 获取创建时间并且转换时间
    currentUserQS.forEach(QS => {
        // console.log(item);
        // console.log(item.createdAt);
        //时间转换函数
        QS.createTime = dateTime(QS.createdAt)
        // console.log(QS.createTime);
    })



    var obj = {
        username,
        currentUserQS
    }

    ctx.render('userPubishQS.html', obj);
};


// 调查问卷详情页面
var fn_QSParticulars = async (ctx, next) => {
    //用户名
    var username = ctx.session.userName;
    //用户id
    var userId = ctx.session.userId;

    //通过当前用户点击详情的问卷Id 查找 用户点击前往的问卷
    //获取当前用户Id所点击的问卷Id
    var userClickQs = await QSSelect.findAll({
        order: [
            ['id', 'desc']
        ],
        where: {
            userId: userId
        }
    });
    // console.log(userClickQs);
    //   根据最后一次点击详情获取调查问卷Id
    var questionnaireId = userClickQs[0].questionnaireId;

    // 根据最后一次点击详情获取调查问卷Id查找该调查问卷题目
    var userClickQstitle = JSON.parse(JSON.stringify(await questionnaire.findOne({
        where: {
            id: questionnaireId
        }
    })));
    // console.log(userClickQstitle);

    // 根据点击详情的问卷id获取到该答案卷
    var userClickQs2 = JSON.parse(JSON.stringify(await questionnaireAnswers.findAll({
        where: {
            questionnaireId: questionnaireId
        }
    })));
    // console.log(userClickQs2);

    //查询用户表的所有用户
    var userAll = JSON.parse(JSON.stringify(await users.findAll({})));
    // console.log(userAll);
    userClickQs2.forEach(userQS => {
        // console.log(userQS.answerUserId);

        var UserNames = userAll.filter(row => {
            return userQS.answerUserId === row.id;
        })[0];
        userQS.UserName = UserNames === undefined ? '匿名' : UserNames.userName;
        // console.log(UserNames.userName);
        // console.log(userQS.UserName);
        // console.log(userQS.topicOneAnswer);
        // 计算总分
        userQS.SQSum = parseInt(userQS.topicOneAnswer) + parseInt(userQS.topicTwoAnswer) + parseInt(userQS.topicThreeAnswer) + parseInt(userQS.topicFourAnswer) + parseInt(userQS.topicFiveAnswer) + parseInt(userQS.topicSixAnswer) + parseInt(userQS.topicSevenAnswer) + parseInt(userQS.topicEightAnswer) + parseInt(userQS.topicNineAnswer) + parseInt(userQS.topicTenAnswer);
        // 计算平均分
        userQS.SQAvg = (parseInt(userQS.topicOneAnswer) + parseInt(userQS.topicTwoAnswer) + parseInt(userQS.topicThreeAnswer) + parseInt(userQS.topicFourAnswer) + parseInt(userQS.topicFiveAnswer) + parseInt(userQS.topicSixAnswer) + parseInt(userQS.topicSevenAnswer) + parseInt(userQS.topicEightAnswer) + parseInt(userQS.topicNineAnswer) + parseInt(userQS.topicTenAnswer)) / 10;

    });

    var obj = {
        username,
        userClickQstitle,
        userClickQs2,
    }

    ctx.render("QSParticulars.html", obj)

};

//用户点击的是哪个表单判断
var fn_userPubishQSJudge = async (ctx, next) => {

    //获取用户点击前往的问卷id
    var qsId = ctx.request.body.qoeId.trim();
    // console.log(qsId);
    //获取点击用户Id
    var userId = ctx.session.userId;

    //将点击详情的问卷id与点击用户Id存入数据库
    await QSSelect.create({
        questionnaireId: qsId,
        userId: userId
    });


    let res = "";
    res = { code: 200 };
    ctx.body = JSON.stringify(res);
};



//已填写的调查问卷
var fn_fillInQS = async (ctx, next) => {
    var username = ctx.session.userName;
    var userId = ctx.session.userId;
    // //查询调查问卷答案的该用户的所有问卷
    var questionnaireAnswersAll = JSON.parse(JSON.stringify(await questionnaireAnswers.findAll({
        where: {
            answerUserId: userId
        }
    })));
    console.log(questionnaireAnswersAll);

    //查询问卷调查名
    var questionnaires = JSON.parse(JSON.stringify(await questionnaire.findAll({})));

    questionnaireAnswersAll.forEach(userQS => {

        // console.log(userQS);
        var userQs = questionnaires.filter(item => {
            return userQS.questionnaireId === item.id
        })[0];
        console.log(userQs);
        console.log(userQs.qsName);
        userQS.UserQSName = userQs === undefined ? '匿名' : userQs.qsName;
    });

    // console.log(questionnaireAnswersAll);


    var obj = {
        username,
        questionnaireAnswersAll
    }

    ctx.render('fillInQS.html', obj);
};





module.exports = {
    '/home/userPubishQS': ['get', fn_userPubishQS],
    '/home/QSParticulars': ['get', fn_QSParticulars],
    '/home/fillInQS': ['get', fn_fillInQS],
    '/home/userPubishQSJudge': ['post', fn_userPubishQSJudge],
}